/**
 * 泛型对象池，提供 warm/get/put，并暴露统计指标。
 */
export class Pool<T> {
  private free: T[] = [];
  private factory: () => T;
  private _total = 0;
  private _created = 0;

  constructor(factory: () => T) {
    this.factory = factory;
  }

  warm(size: number): void {
    for (let i = this.free.length; i < size; i++) {
      this.free.push(this.create());
    }
  }

  get(): T {
    if (this.free.length === 0) {
      return this.create();
    }
    return this.free.pop() as T;
  }

  put(item: T): void {
    this.free.push(item);
  }

  get total(): number {
    return this._total;
  }

  get freeCount(): number {
    return this.free.length;
  }

  get createdCount(): number {
    return this._created;
  }

  private create(): T {
    const item = this.factory();
    this._total++;
    this._created++;
    return item;
  }
}

